﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.Car_Li;
using ICarlifeRespository;
using Commonality.DTO.Car_Li;
using Carlife.Inf;
using System.Collections;
using ICarlifeService;

namespace CarlifeService
{
    public class CarListService: BaseServer<CarMarket>,ICarListService
    {
        private readonly ICarMarketRespository db;
        private readonly IClientdisposeRespository clientdispose;
        private readonly IDictionariesRespository dictionaries;
        private readonly ISalesperformanceRespository salesperformance;
        private readonly IUOfWork uOfWork;
        public CarListService(ICarMarketRespository db, IUOfWork uOfWork, IClientdisposeRespository clientdispose, IDictionariesRespository dictionaries, ISalesperformanceRespository salesperformance) : base(db, uOfWork)
        {
            this.db = db;
            this.clientdispose = clientdispose;
            this.dictionaries = dictionaries;
            this.salesperformance = salesperformance;
        }

        public CarSalesFenye Show( string? name = "", string ctime = "", string Utime = "", int pagesize = 3, int pageindex = 1)
        {
            var list = from a in dictionaries.GetAll()
                       join b in db.GetAll() on a.Tid equals b.Tid
                       join c in salesperformance.GetAll() on b.SalesId equals c.Id
                       join d in clientdispose.GetAll() on c.Clientd equals d.Id
                       select new CarSalesDTO
                       {
                           MarketId = b.Id,
                           MarketName = b.MarketName,
                           MarketSeries = b.MarketSeries,
                           Tname = a.Tname,
                           SalesNumber = c.SalesNumber,
                           Marketcard = b.Marketcard,
                           MarketEngine = b.MarketEngine,
                           MarketCreateDate = b.MarketCreateDate,
                           MarketUpdateDate = b.MarketUpdateDate,
                           MarketNum = b.MarketNum,
                           MarketColor = b.MarketColor,
                       };
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.MarketName.Contains(name));
            }
            if (!string.IsNullOrEmpty(ctime))
            {
                list = list.Where(x => x.MarketCreateDate >= (Convert.ToDateTime(ctime)));
            }
            if (!string.IsNullOrEmpty(Utime))
            {
                list = list.Where(x => x.MarketUpdateDate <= (Convert.ToDateTime(Utime)));
            }
            return new CarSalesFenye
            {
                Rowcount = list.Count(),
                CData = list.OrderBy(x => x.MarketId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList()
            };


        }
        
    }
}
